{
  "name": "Audit Observation Tracking Agent",
  "nodes": [
    {
      "parameters": {
        "updates": ["message"]
      },
      "id": "tg-trigger-audit",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [0, 0]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "name": "user_message",
              "value": "={{ $json.message.text }}",
              "type": "string"
            },
            {
              "name": "user_name",
              "value": "={{ $json.message.from.first_name + ' ' + ($json.message.from.last_name || '') }}",
              "type": "string"
            },
            {
              "name": "chat_id",
              "value": "={{ $json.message.chat.id }}",
              "type": "string"
            }
          ]
        }
      },
      "id": "set-audit-context",
      "name": "Workflow Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [220, 0]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=User Name: {{ $json.user_name }}\nUser Message: {{ $json.user_message }}\nChat ID: {{ $json.chat_id }}",
        "options": {
          "systemMessage": "=You are an Audit Observation Tracking Assistant.\n\nYour responsibilities:\n1. Identify whether the user wants to REGISTER a new audit observation or CHECK observation status.\n\n2. For new audit observations, collect:\n   - Audit Reference / Report ID\n   - Process / Department Name\n   - Observation Category (Control Lapse, Compliance Issue, Process Gap, Documentation Issue)\n   - Brief description of observation\n\n3. Validate the audit reference using the Audit Master Tool and extract:\n   - Process_Owner\n   - Owner_Email (for communication)\n   - Audit_Status\n\n4. If the audit reference is valid and open:\n   - Generate an observation reference ID in format:\n     AUD-YYYYMMDD-[last 4 digits of audit]-HHmmss\n   - Register the observation using the Audit Observation Register Tool\n   - Send acknowledgement email using Gmail Tool to Owner_Email\n   - Explain closure and review timelines\n   - Respond using Telegram Response Tool\n\n5. If the audit reference is invalid or closed:\n   - Politely explain the reason\n   - Do NOT register the observation\n   - Respond using Telegram Response Tool\n\n6. For status checks:\n   - Ask for observation reference ID if missing\n   - Retrieve status using the Audit Observation Status Tool\n   - Explain current closure stage and next steps\n   - Respond using Telegram Response Tool\n\nStrict rules:\n- Do NOT provide legal advice.\n- Do NOT promise closure timelines.\n- Email must be retrieved from master sheet only.\n- ALWAYS respond using the Telegram Response Tool."
        }
      },
      "id": "agent-audit",
      "name": "Audit Observation Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3,
      "position": [500, 0]
    },
    {
      "parameters": {
        "model": { "mode": "id", "value": "gpt-4o" },
        "options": { "temperature": 0.2 }
      },
      "id": "llm-audit",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [320, 220]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Workflow Configuration').item.json.chat_id }}"
      },
      "id": "memory-audit",
      "name": "Simple Memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [500, 220]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "audit-master",
      "name": "Audit Master Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [640, 260]
    },
    {
      "parameters": {
        "operation": "append",
        "descriptionType": "manual"
      },
      "id": "audit-register",
      "name": "Audit Observation Register Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [800, 260]
    },
    {
      "parameters": {
        "descriptionType": "manual"
      },
      "id": "audit-status",
      "name": "Audit Observation Status Tool",
      "type": "n8n-nodes-base.googleSheetsTool",
      "typeVersion": 4.7,
      "position": [960, 320]
    },
    {
      "parameters": {
        "sendTo": "={{ $fromAI('owner_email') }}",
        "subject": "Audit Observation Registered",
        "message": "An audit observation has been logged against your process and is under review."
      },
      "id": "gmail-audit",
      "name": "Audit Observation Email Notification",
      "type": "n8n-nodes-base.gmailTool",
      "typeVersion": 2.1,
      "position": [1120, 220]
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}"
      },
      "id": "tg-response-audit",
      "name": "Send Telegram Response",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [960, 0]
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [[{ "node": "Workflow Configuration", "type": "main" }]]
    },
    "Workflow Configuration": {
      "main": [[{ "node": "Audit Observation Agent", "type": "main" }]]
    },
    "Audit Observation Agent": {
      "main": [[{ "node": "Send Telegram Response", "type": "main" }]]
    }
  },
  "active": false
}
